/**
  ******************************************************************************
  * @file           : banyan.c
  * @author         : iclm team
  * @brief          : banyan driver
  ******************************************************************************
  */
#include <stdio.h>
#include "global_conf.h"
#include "platform.h"
#include "banyan.h"
#include "ft_config.h"

#include <hosal_i2c.h>
#include "bsp_iic.h"
#include "bsp_nvs.h"
#include "mcu_sleep.h"


static uint8_t radarRegNeedUpDate = 0;

#if 0
#if USE_FULL_FRAME
static RADAR_REG_T InitRegList[MAX_REG_NUM] __attribute__((aligned (4))) =
{
    {0x40, 0x4207},
    {0x41, 0x0004},
    {0x01, 0x0000},
    {0x67, 0x0000},
    {0x72, 0x0650},
    {0x42, 0x0001},
    {0x43, 0xD4C0},
    // T3 6ms
    // {0x42, 0x0014},
    // {0x43, 0x1C70},
    {0x44, 0x0022},
    {0x45, 0x0000},
    {0x46, 0x0FA0},
    {0x47, 0x1001},
    {0x48, 0x57C0},
    {0x49, 0x2000},
    {0x4A, 0x55F0},
    {0x4B, 0x0000},
    {0x4C, 0x1770},
    // T3 6ms
    // {0x4B, 0x0012},
    // {0x4C, 0x4F80},
    {0x4D, 0x0000},
    {0x4E, 0x0001},
    {0x4F, 0x0001},
    {0x50, 0x86A0},
    // {0x51, 0x0003},
    // {0x52, 0x0D40},
    // T_NOP 100ms
    // {0x51, 0x0131},
    // {0x52, 0x2D00},
    // T_NOP 30ms
    {0x51, 0x005B},
    {0x52, 0x8D80},
    {0x53, 0x0A02},
    {0x54, 0xAAAB},
    {0x55, 0x0000},
    {0x56, 0x0011},
    {0x57, 0xFFFF},
    {0x58, 0xFFBC},
    {0x59, 0x0000},
    {0x5A, 0x0000},
    {0x5B, 0x0022},
    {0x5C, 0x0022},
    {0x5D, 0x1919},
    {0x5E, 0xFF00},
    {0x61, 0x02B5},
    {0x62, 0x02B5},
    {0x63, 0x02B5},
    {0x64, 0x02B5},
    {0x6E, 0x83FC},
    {0x66, 0x0A00},
    {0x6C, 0x9990},
    {0x6D, 0x9580},
    {0x70, 0x2EA0},
    {0x76, 0x0021},
    {0x02, 0x2064},
    {0x04, 0x020C},
    {0x09, 0x6901},
    {0x0A, 0xC200},
    {0x0B, 0xC064},
    {0x05, 0x000A},
    {0x06, 0x0123},
    {0x0D, 0x1E00},
    {0x0E, 0x2200},
    {0x14, 0x5A03},
    {0x15, 0x1708},
    {0x17, 0x0210},
    {0x20, 0x0000},
    {0x21, 0x0000},
    {0x22, 0x0000},
    {0x23, 0x1DDC},
    {0x24, 0x1D5E},
    {0x25, 0x1CE2},
    {0x26, 0x1C64},
    {0x27, 0x17D0},
    {0x28, 0x16D4},
    {0x29, 0x15DC},
    {0x2A, 0x15DC},
    {0x2B, 0x15DC},
    {0x2C, 0x15DC},
    {0x2D, 0x15DC},
    {0x2E, 0x15DC},
    {0x2F, 0x15DC},
    {0x72, 0x0793},
    {0x67, 0x1840},
    {0x01, 0x8E24},
    {0x41, 0xC844},
    {0x40, 0x0207},
    {0x00, 0x0000} /*must be last, do not delete!!!*/
};
#else
static RADAR_REG_T InitRegList[MAX_REG_NUM] __attribute__((aligned (4))) =
{
    {0x40, 0x4207},
    {0x41, 0x0004},
    {0x01, 0x0000},
    {0x67, 0x0000},
    {0x72, 0x0650},
    // {0x42, 0x0001},
    // {0x43, 0xD4C0},
    // T3 6ms
    {0x42, 0x0014},
    {0x43, 0x1C70},
    {0x44, 0x0022},
    {0x45, 0x0000},
    {0x46, 0x0FA0},
    {0x47, 0x1001},
    {0x48, 0x57C0},
    {0x49, 0x2000},
    {0x4A, 0x55F0},
    // {0x4B, 0x0000},
    // {0x4C, 0x1770},
    // T3 6ms
    {0x4B, 0x0012},
    {0x4C, 0x4F80},
    {0x4D, 0x0000},
    {0x4E, 0x0001},
    {0x4F, 0x0001},
    {0x50, 0x86A0},
    // {0x51, 0x0003},
    // {0x52, 0x0D40},
    // T_NOP 100ms
    // {0x51, 0x0131},
    // {0x52, 0x2D00},
    // T_NOP 30ms
    // {0x51, 0x005B},
    // {0x52, 0x8D80},
    // T_NOP 10ms
    {0x51, 0x001E},
    {0x52, 0x8480},
    {0x53, 0x0A02},
    {0x54, 0xAAAB},
    {0x55, 0x0000},
    {0x56, 0x0011},
    {0x57, 0xFFFF},
    {0x58, 0xFFBC},
    {0x59, 0x0000},
    {0x5A, 0x0000},
    {0x5B, 0x0022},
    {0x5C, 0x0022},
    {0x5D, 0x1919},
    {0x5E, 0xFF00},
    {0x61, 0x02B5},
    {0x62, 0x02B5},
    {0x63, 0x02B5},
    {0x64, 0x02B5},
    {0x6E, 0x83FC},
    {0x66, 0x0A00},
    {0x6C, 0x9990},
    {0x6D, 0x9580},
    {0x70, 0x2EA0},
    {0x76, 0x0021},
    {0x02, 0x2064},
    {0x04, 0x020C},
    {0x09, 0x6901},
    {0x0A, 0xC200},
    {0x0B, 0xC064},
    {0x05, 0x000A},
    {0x06, 0x0123},
    {0x0D, 0x1E00},
    {0x0E, 0x2200},
    {0x14, 0x5A03},
    {0x15, 0x1708},
    {0x17, 0x0210},
    {0x20, 0x0000},
    {0x21, 0x0000},
    {0x22, 0x0000},
    {0x23, 0x1DDC},
    {0x24, 0x1D5E},
    {0x25, 0x1CE2},
    {0x26, 0x1C64},
    {0x27, 0x17D0},
    {0x28, 0x16D4},
    {0x29, 0x15DC},
    {0x2A, 0x15DC},
    {0x2B, 0x15DC},
    {0x2C, 0x15DC},
    {0x2D, 0x15DC},
    {0x2E, 0x15DC},
    {0x2F, 0x15DC},
    {0x72, 0x0793},
    {0x67, 0x1840},
    {0x01, 0x8E24},
    {0x41, 0xC844},
    {0x40, 0x0207},
    {0x00, 0x0000} /*must be last, do not delete!!!*/
};
#endif
#endif
#define FT_USE_CHIRP (0)

#if FT_USE_CHIRP
static RADAR_REG_T InitRegList[MAX_REG_NUM] __attribute__((aligned (4))) =
{
    {0x40, 0x4207},
    {0x41, 0x0004},
    {0x01, 0x0000},
    {0x67, 0x0000},
    {0x72, 0x0650},
    // {0x42, 0x0001},
    // {0x43, 0x1940},
    // T3 6ms
    // {0x42, 0x0014},
    // {0x43, 0x1C70},
    // T3 7ms
    {0x42, 0x0017},
    {0x43, 0x29B0},
    {0x44, 0x0040},
    {0x45, 0x0000},
    {0x46, 0x0FA0},
    {0x47, 0x1000},
    {0x48, 0xABE0},
    {0x49, 0x2000},
    {0x4A, 0x2EE0},
    // {0x4B, 0x0000},
    // {0x4C, 0x2EE0},
    // T3 6ms
    // {0x4B, 0x0012},
    // {0x4C, 0x4F80},
    // T3 7ms
    {0x4B, 0x0015},
    {0x4C, 0x5CC0},
    {0x4D, 0x0000},
    {0x4E, 0x0001},
    {0x4F, 0x0000},
    {0x50, 0x0FA0},
    // {0x51, 0x0070},
    // {0x52, 0xEA40},
    // T_NOP 10ms
    // {0x51, 0x001E},
    // {0x52, 0x8480},
    // T_NOP 6ms
    // {0x51, 0x0012},
    // {0x52, 0x4F80},
    // T_NOP 7ms
    {0x51, 0x0015},
    {0x52, 0x5CC0},
    {0x53, 0x09CA},
    {0x54, 0xAAAB},
    {0x55, 0x0000},
    {0x56, 0x00BF},
    {0x57, 0xFFFF},
    {0x58, 0xFD44},
    {0x59, 0x0000},
    {0x5A, 0x0000},
    {0x5B, 0x0022},
    {0x5C, 0x0022},
    {0x5D, 0x1919},
    {0x5E, 0xFF00},
    {0x61, 0x0021},
    {0x62, 0x0021},
    {0x63, 0x0021},
    {0x64, 0x0021},
    {0x66, 0x0A00},
    {0x6C, 0x9990},
    {0x6D, 0x9DC0},
    {0x6E, 0x83FC},
    {0x70, 0x3EA0},
    {0x76, 0x0021},
    {0x06, 0x0124},
    {0x02, 0x1032},
    {0x04, 0x020C},
    {0x09, 0x6901},
    {0x0A, 0x4200},
    {0x0B, 0xC032},
    {0x05, 0x0008},
    {0x0D, 0x0800},
    {0x0E, 0x4000},
    {0x15, 0x1708},
    {0x14, 0x5A03},
    {0x17, 0x0210},
    {0x20, 0x0000},
    {0x21, 0x0000},
    {0x22, 0x0000},
    {0x23, 0x1DDC},
    {0x24, 0x1D5E},
    {0x25, 0x1CE2},
    {0x26, 0x1C64},
    {0x27, 0x17D0},
    {0x28, 0x16D4},
    {0x29, 0x15DC},
    {0x2A, 0x15DC},
    {0x2B, 0x15DC},
    {0x2C, 0x15DC},
    {0x2D, 0x15DC},
    {0x2E, 0x15DC},
    {0x2F, 0x15DC},
    {0x72, 0x0653},
    {0x67, 0x1840},
    {0x01, 0x8E24},
    {0x41, 0xC854},
    {0x40, 0x0207},
    {0x00, 0x0000} /*must be last, do not delete!!!*/
};
#else
static RADAR_REG_T InitRegList[MAX_REG_NUM] __attribute__((aligned (4))) =
{
    {0x40, 0x4207},
    {0x41, 0x0004},
    {0x01, 0x0000},
    {0x67, 0x0000},
    {0x72, 0x0650},
    {0x42, 0x0001},
    {0x43, 0x1940},
    {0x44, 0x0040},
    {0x45, 0x0000},
    {0x46, 0x0FA0},
    {0x47, 0x1000},
    {0x48, 0xABE0},
    {0x49, 0x2000},
    {0x4A, 0x2EE0},
    {0x4B, 0x0000},
    {0x4C, 0x2EE0},
    {0x4D, 0x0000},
    {0x4E, 0x0001},
    {0x4F, 0x0000},
    {0x50, 0x0FA0},
    {0x51, 0x0070},
    {0x52, 0xEA40},
    {0x53, 0x09CA},
    {0x54, 0xAAAB},
    {0x55, 0x0000},
    {0x56, 0x00BF},
    {0x57, 0xFFFF},
    {0x58, 0xFD44},
    {0x59, 0x0000},
    {0x5A, 0x0000},
    {0x5B, 0x0022},
    {0x5C, 0x0022},
    {0x5D, 0x1919},
    {0x5E, 0xFF00},
    {0x61, 0x0021},
    {0x62, 0x0021},
    {0x63, 0x0021},
    {0x64, 0x0021},
    {0x66, 0x0A00},
    {0x6C, 0x9990},
    {0x6D, 0x9DC0},
    {0x6E, 0x83FC},
    {0x70, 0x3EA0},
    {0x76, 0x0021},
    {0x06, 0x0124},
    // {0x06, 0x11FE},     // 1.538MHz
    {0x02, 0x1032},
    {0x04, 0x020C},
    {0x09, 0x6901},
    {0x0A, 0x4200},
    {0x0B, 0xC032},
    {0x05, 0x0008},
    {0x0D, 0x0800},
    {0x0E, 0x4000},
    {0x15, 0x1708},
    {0x14, 0x5A03},
    {0x17, 0x0210},
    {0x20, 0x0000},
    {0x21, 0x0000},
    {0x22, 0x0000},
    {0x23, 0x1DDC},
    {0x24, 0x1D5E},
    {0x25, 0x1CE2},
    {0x26, 0x1C64},
    {0x27, 0x17D0},
    {0x28, 0x16D4},
    {0x29, 0x15DC},
    {0x2A, 0x15DC},
    {0x2B, 0x15DC},
    {0x2C, 0x15DC},
    {0x2D, 0x15DC},
    {0x2E, 0x15DC},
    {0x2F, 0x15DC},
    {0x72, 0x0653},
    {0x67, 0x1840},
    {0x01, 0x8E24},
    {0x41, 0xC854},
    {0x40, 0x0207},
    {0x00, 0x0000} /*must be last, do not delete!!!*/
};
#endif

static uint16_t rawPointMap[RAW_MAP_NUM] = 
{
	RAW_POINT_64,
    RAW_POINT_128,
    RAW_POINT_256,
    RAW_POINT_512,
    RAW_POINT_1024
};

static const RADAR_REG_T RegList_LowPWR[] =
{
	{0x70, 0x1020},
	{0x6C, 0x8880},
	{0x6D, 0x8800},
	{0x72, 0x0650},
	{0x67, 0x0000},
	{0x66, 0xF0F0},
	{0x6E, 0x03FC},
	{0x41, 0x4804},
	{0x00, 0x0000} 
};

static const RADAR_REG_T RegList_NormalPWR[] =
{
	{0x41, 0xc864},
	{0x72, 0x0653},
	{0x6C, 0x9990},
	{0x6D, 0x9940},
	{0x70, 0x32a0},
	{0x6E, 0xabFC},
	{0x66, 0x0a00},
	{0x67, 0x1840},
	{0x00, 0x0000} 
};

void Radar_SaveRegList(void)
{
    uint16_t loop = 0;
    
    if (!radarRegNeedUpDate)
    {
        return;
    }

    while(InitRegList[loop].addr) 
    {
        loop++;
    }
    loop++;
    
    Config_WriteRadarPara2Flash((uint32_t*)InitRegList, loop * sizeof(RADAR_REG_T));
    radarRegNeedUpDate = 0;
}

void Ft_Radar_Init(void)
{
    uint16_t loop = 0;
    uint32_t paraLen = 0;

    paraLen = Config_ReadRadarParaLen();

    if (paraLen == 0 || paraLen > ((64+16) * sizeof(RADAR_REG_T)))
    {
        radarRegNeedUpDate = 1;
        Radar_SaveRegList();
        paraLen = Config_ReadRadarParaLen();
    }
    
    printf("paraLen: 0x%x\r\n", paraLen);
    Config_ReadRadarParaData((uint32_t*)InitRegList, paraLen);
    
#if 1
    while(InitRegList[loop].addr)
    {
        printf("0x%02x:0x%04x\r\n", InitRegList[loop].addr, InitRegList[loop].val);
        loop++;
    }
    loop = 0;
#endif

    while(InitRegList[loop].addr) 
    {
        bsp_iic_write(I2C_ADDR_BanYan_Chip0, (uint8_t)(InitRegList[loop].addr), InitRegList[loop].val);
        loop++;
    }

    Delay(31);
}

void Ft_WriteSocAllReg(void)
{
    uint16_t loop = 0;
    
    while(InitRegList[loop].addr) 
    {
        bsp_iic_write(I2C_ADDR_BanYan_Chip0, (uint8_t)(InitRegList[loop].addr), InitRegList[loop].val);
        loop++;
    }
}

/********************************************
 @名称；setSOCEnterNormalMode
 @功能：打开SOC的供电，在一段时间延迟后写全部寄存器
 @参数：无
 @返回：无
 @作者：AE TEAM
*********************************************/
void FT_setSOCEnterNormalMode(void)
{
    power_on();
    Delay(10);
    Ft_WriteSocAllReg();
}
